using LitJson;
using UnityEngine;
using UnityEngine.UI;
namespace CGF
{
    public partial class PanelMyExpoDetail : UIComponentBase, IAwake<UILayer, bool>, IDestroy
    {
        public static void Open(JsonData data)
        {
            UIManagerComponent.instance.OpenUI<PanelMyExpoDetail>(UILayer.Normal, RootComponent.instance, true).SetData(data);
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<PanelMyExpoDetail>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/GameUI/PanelMyExpoDetail", layer, isFloat);
            BindProperties(transform);
            
            RootComponent.instance.GetComponent<PanelMyExpo>().ScrollView.enabled = false;

            BtnClose.SetClick(Close);
            MsgCellAI.gameObject.SetActive(false);
            MsgCellMe.gameObject.SetActive(false);
        }

        private void SetData(JsonData data)
        {
            string color = "#ffcc00";
            TextScroe.text = $"{I18N_scroe}：<color={color}>{data["score"]}</color>";
            TextTime.text = $"{I18N_time}：<color={color}>{TimeHelper.Parse2Time(data["created_at"].ToString(), true)}</color>";
            TextContent.text = $"{I18N_content}：<color={color}>{data["content"]}</color>";
            string chats = data["chats"].ToString();
            JsonData msgs = JsonMapper.ToObject(chats);
            for (int i = 0; i < msgs.Count; i++)
            {
                bool isMe = msgs[i]["senderId"].ToString() == UserInfo.UserId;
                Text cell = GameObject.Instantiate(isMe ? MsgCellMe : MsgCellAI, MsgCellMe.transform.parent, false);
                cell.gameObject.SetActive(true);
                cell.text = StringHelper.SurportSpace(msgs[i]["content"].ToString().Trim().Replace("\n", ""));
            }
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
            RootComponent.instance.GetComponent<PanelMyExpo>().ScrollView.enabled = true;
        }

    }
}
